﻿/**
 * ...
 * @author Ellis
 */

package getengine.core.math;

class Matrix3
{
		
	public var _11 : Float;
	public var _12 : Float;
	public var _13 : Float;

	public var _21 : Float;
	public var _22 : Float;
	public var _23 : Float;

	public var _31 : Float;
	public var _32 : Float;
	public var _33 : Float;
	
	public function new() 
	{
		
	}
	
	public function initIdentity(): Matrix3
	{
		_11 = 1.0; _12 = 0.0; _13 = 0.0; 
		_21 = 0.0; _22 = 1.0; _23 = 0.0;
		_31 = 0.0; _32 = 0.0; _33 = 1.0;
		
		return this;
	}
	
	
	public function initZero(): Matrix3
	{
		_11 = 0.0; _12 = 0.0; _13 = 0.0; 
		_21 = 0.0; _22 = 0.0; _23 = 0.0;
		_31 = 0.0; _32 = 0.0; _33 = 0.0;
		
		return this;
	}

	public function initRotation(angle: Float): Matrix3
	{
		var cos = Math.cos(angle);
		var sin = Math.sin(angle);
		_11 = cos;  _12 = sin;// _13 = 0.0;
		_21 = -sin; _22 = cos;// _23 = 0.0;
		//_31 = 0.0;  _32 = 0.0; _33 = 1.0;
		
		return this;
	}
	
	public function concat(m: Matrix3, ?concatType: MatrixConcatinationType) : Matrix3
	{
		if (concatType == null)
			concatType = MatrixConcatinationType.PostConcat;
		
			
		var ma11: Float = 0.0; 
		var ma12: Float = 0.0; 
		var ma13: Float = 0.0; 
		var ma21: Float = 0.0; 
		var ma22: Float = 0.0; 
		var ma23: Float = 0.0; 
		var ma31: Float = 0.0; 
		var ma32: Float = 0.0; 
		var ma33: Float = 0.0;
		var mb11: Float = 0.0; 
		var mb12: Float = 0.0; 
		var mb13: Float = 0.0; 
		var mb21: Float = 0.0; 
		var mb22: Float = 0.0; 
		var mb23: Float = 0.0; 
		var mb31: Float = 0.0; 
		var mb32: Float = 0.0; 
		var mb33: Float = 0.0;
		
		switch(concatType)
		{
			case MatrixConcatinationType.PostConcat:
			ma11 = m._11; ma12 = m._12; ma13 = m._13; 
			ma21 = m._21; ma22 = m._22; ma23 = m._23; 
			ma31 = m._31; ma32 = m._32; ma33 = m._33; 
			mb11 = this._11; mb12 = this._12; mb13 = this._13;
			mb21 = this._21; mb22 = this._22; mb23 = this._23; 
			mb31 = this._31; mb32 = this._32; mb33 = this._33; 
			case MatrixConcatinationType.PreConcat:
			mb11 = m._11; mb12 = m._12; mb13 = m._13; 
			mb21 = m._21; mb22 = m._22; mb23 = m._23; 
			mb31 = m._31; mb32 = m._32; mb33 = m._33; 
			ma11 = this._11; ma12 = this._12; ma13 = this._13;
			ma21 = this._21; ma22 = this._22; ma23 = this._23; 
			ma31 = this._31; ma32 = this._32; ma33 = this._33;
		}
		
		_11 = ma11 * mb11 + ma12 * mb21 + ma13 * mb31;
		_12 = ma11 * mb12 + ma12 * mb22 + ma13 * mb32;
		_13 = ma11 * mb13 + ma12 * mb23 + ma13 * mb33;
		
		_21 = ma21 * mb11 + ma22 * mb21 + ma23 * mb31;
		_22 = ma21 * mb12 + ma22 * mb22 + ma23 * mb32;
		_23 = ma21 * mb13 + ma22 * mb23 + ma23 * mb33;
		
		_31 = ma31 * mb11 + ma32 * mb21 + ma33 * mb31;
		_32 = ma31 * mb12 + ma32 * mb22 + ma33 * mb32;
		_33 = ma31 * mb13 + ma32 * mb23 + ma33 * mb33;
				
		return this;
	}
	
	public function toString(): String
	{
		return "Matrix3[" + _11 + ", " + _12 + ", " + _13 + "]\n" +
		       "       [" + _21 + ", " + _22 + ", " + _23 + "]\n" +
		       "       [" + _31 + ", " + _32 + ", " + _33 + "]";
	}
}